<?php
//+-----------------------------------------------------------
//| 人生是荒芜的旅行，冷暖自知，苦乐在心
//+-----------------------------------------------------------
//| Author:Janmas <janmas@126.com>
//+-----------------------------------------------------------
//| 
//+-----------------------------------------------------------


namespace app\common\taglib;

/**
 * 自定义标签库
 * @package app\common\taglib
 */
class Tag extends \think\template\TagLib
{
    protected $tags = [
        'menu' => [
            'close' => 0
        ],
        'currentmenu' => [
            'close' => 0,
        ]
    ];

    /**
     * 菜单标签
     * @param $tag
     * @return string
     */
    public function tagMenu($tag){
        $parse = '<?php ';
        $parse .= ' $menus = (new \\app\\admin\\service\\Menu())->getTreeMenu();';
        $parse .= ' if(!empty($menus)) :';
        $parse .= '     foreach( $menus as $value): ';
        $parse .= '         if(empty($value["link"])):';
        $parse .= '            echo "<li class=\"layui-nav-item\"><a href=\"javascript:;\"><i class=\"layui-icon\">" . $value["icon"] . "</i>&nbsp;&nbsp;".$value["name"]."</a>";';
        $parse .= '         else:';
        $parse .= '             if ($value["flag"] != "admin"): $link =  "/plugins/".$value["flag"] ."/admin/".$value["link"];else: $link = url($value["link"]);endif;';
        $parse .= '            echo "<li class=\"layui-nav-item\"><a href=\"" . $link . "\"><i class=\"layui-icon\">" . $value["icon"] . "</i>&nbsp;&nbsp;".$value["name"]."</a>";';
        $parse .= '         endif;';
        $parse .= '         echo "<dl class=\"layui-nav-child\">";';
        $parse .= '         if(!empty($value["children"])):';
        $parse .= '             foreach($value["children"] as $v):';
        $parse .= '                 if($v["flag"] != "admin"):$link =  "/plugins/".$v["flag"] ."/admin/".$v["link"];else: $link = url($v["link"]); endif;';
        $parse .= '                 echo "<dd><a href=\"javascript:;\" class=\"site-demo-active\" lay-id=\"".$v["id"]."\" lay-href=\"".$link."\"><i class=\"layui-icon\">" . $v["icon"] . "</i>&nbsp;&nbsp;".$v["name"]."</a></dd>";';
        $parse .= '             endforeach;';
        $parse .= '         endif;';
        $parse .= '         echo "</dl></li>";';
        $parse .= '     endforeach;';
        $parse .= ' endif;';
        $parse .= ' ?>';
        return $parse;


    }

    /**
     * 获取当前菜单名做面包屑
     * @param $tag
     * @return string
     */
    public function tagCurrentMenu($tag){
        $parse = ' <?php ';
        $parse .= ' $mianbaoxie = "";';
        $parse .= ' $menus = (new \\app\\admin\\service\\Menu())->getCurrentMenu();';
        $parse .= ' if(!empty($menus)):';
        $parse .= ' foreach($menus as $menu):';
        $parse .= ' $mianbaoxie .= "<a href=\"" . url($menu["link"]) ."\">" . $menu["name"] . "</a>";';
        $parse .= ' endforeach;';
        $parse .= ' echo "<span class=\"layui-breadcrumb\">".$mianbaoxie."</span>";';
        $parse .= ' endif; ?>';
        return $parse;
    }

}